<?php

use console\db\Migration;
use yii\db\Query;
use yii\helpers\Console;

/**
 * 更新动态格式:单图
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300020_000018_t_sns_dynamic_gallery_unique extends Migration {

    /**
     * @inheritdoc
     */
    public $tableName = '{{%sns_dynamic_gallery}}';

    /**
     * @inheritdoc
     */
    public function safeUp() {
        $query = (new Query())->from($this->tableName)
        ->select(['id'])
        ->where(['comment_id' => 0])
        ->groupBy(['original', 'dynamic_id'])
        ->having('count(*)>1');
        $this->handleProgress($query, [$this, 'evenDule']);
    }

    /**
     * 处理结果
     * @param Migration $migration
     * @param array $rows
     */
    public static function evenDule($migration, $rows) {
        if (empty($rows)) {
            return;
        }
        foreach ($rows as $row) {
            $migration->delete($migration->tableName, ['id' => $row['id']]);
        }
    }

}
